package models

import (
	"log"
	"time"
)

type OaOrder struct {
	Id       int       `json:"id" xorm:"not null pk autoincr INT(11)"`
	SchoolId int       `json:"school_id" xorm:"INT(11)"`
	Number   int       `json:"number" xorm:"int(11,1)"`
	UserId   int       `json:"user_id" xorm:"INT(11)"`
	Addtime  time.Time `json:"addtime" xorm:"comment('下单日期') DATETIME"`
}

type OrderList struct {
	Id         int    `json:"id"`
	SchoolName string `json:"school_name"`
	NameShort  string `json:"name_short"`
	Name       string `json:"name"`
	Number     int    `json:"number"`
	Addtime    string `json:"addtime"`
}

func (c *OaOrder) GetPayStudentList(userId string, pageSize, offset int) (list []*OrderList, count int64, err error) {
	err = engine.Table("oa_order").Alias("co").
		Select("co.id,sch.name as school_name,sch.name_short,co.number,cu.name,co.addtime").
		Join("left", []string{"dts_school", "sch"}, "sch.id = co.school_id").
		Join("left", []string{"caiwu_user", "cu"}, "co.user_id = cu.id").
		Where("co.user_id = ?", userId).
		Limit(pageSize, offset).
		Desc("co.id").
		Find(&list)
	count, _ = engine.Table("oa_order").Alias("co").
		Join("left", []string{"dts_school", "sch"}, "sch.id = co.school_id").
		Where("co.user_id = ?", userId).
		Count()
	return list, count, err
}
func (c *OaOrder) Save() bool {
	var (
		err error
	)
	_, err = engine.Insert(c)
	if err != nil {
		log.Println(err)
		return false
	}
	return true
}
